import sessionStorage from '@/utils/sessionStorage';
import theme from '@/uni.scss'
import settings from '@/config'

let cur = {
  theme: {...theme},
  settings: {...settings},
};
/** 获取本地配置缓存*/
let storage = (key) => {
  sessionStorage.get(key).then($data => {
    state[key] = $data.data;
  }).catch(() => { // 不存在
    state[key] = cur[key];
  });
};
let state = {
  theme: storage('theme'),
  settings: storage('settings')
};


const mutations = {
  /** 改变值*/
  CHANGE_VAL: (state, data) => {
    for (let ol in data['val']) {
      if (state[data.key].hasOwnProperty(ol)) {
        state[data.key][ol] = data['val'][ol];
      }
    }
    /** 配置存起来*/
    sessionStorage.set(data.key, state[data.key]);
  },
};

const actions = {
  /** 改变值*/
  handleChangeVal({commit}, data) {
    if (data.key && data.hasOwnProperty('val')) { // state的key val值列表
      commit('CHANGE_VAL', data);
    }
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

